package com.dbpediamobile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.Toast;

//Activity que muestra el grafo y maneja las opciones del menu
public class Grafo extends Activity{

	GrafoView grafoV;
	@Override
	public void onCreate(Bundle savedInstanceState) {

	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.grafo);
	  FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
	  grafoV =new GrafoView(this);
	  main.addView(grafoV);
	  

	}
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	//agrega los menus directamente desde main_menu.xml
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.grafo_menu, menu);
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.grafo_goBackMapa:         
        	Intent intent = new Intent(Grafo.this, DBPediaMobile.class);
        	startActivity(intent); 
        	break; 
        //busca los siguientes 8 nodos para mostrar
        case R.id.menu_siguiente:
        	if(!grafoV.next())
        		Toast.makeText(getApplicationContext(), "There is no more results", Toast.LENGTH_LONG).show();
        	break;
        //busca los anteriores 8 nodos para mostrar
        case R.id.menu_volver:
        	if(!grafoV.prev())
        		Toast.makeText(getApplicationContext(), "There is no more elements", Toast.LENGTH_LONG).show();
        	break;
        //vuelve al nodo anterior
        case R.id.menu_atras_nodo:
        	if(!grafoV.back())
        		Toast.makeText(getApplicationContext(), "There is no other node", Toast.LENGTH_LONG).show();
        	break;
        //guarda el grafo como imgen
        case R.id.menu_grafo_img:
        	try {
				saveAsImage(grafoV);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				Toast.makeText(getBaseContext(),"Error saving image: "+e.getMessage(), Toast.LENGTH_SHORT).show();
			}
        }
        return false;
    }
    private void saveAsImage(View view) throws IOException{
    	Bitmap image = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
    	view.draw(new Canvas(image));
        String path1 = "/data/data/com.dbpediamobile/files";
        OutputStream fOut = null;
        String name=grafoV.getInput()+" "+System.currentTimeMillis()+".JPEG";
        File file1 = new File(path1, name);
        fOut = new FileOutputStream(file1);

        image.compress(Bitmap.CompressFormat.JPEG, 100, fOut );
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(getContentResolver(),file1.getAbsolutePath(),file1.getName(),file1.getName());
        Toast.makeText(getBaseContext(),"File "+name+" was successfully saved", Toast.LENGTH_SHORT).show();    
    }
    
}
